#ifndef __HANDLER_MESSAGE_HPP__
#define __HANDLER_MESSAGE_HPP__

#include <string>
#include <cstdio>
#include <cstdarg>

namespace log_system
{
    class HandlerMessage
    {
    private:
        static std::string _base_name;

    public:
        enum HandlerLevel
        {
            DEBUG = 0,
            INFO,
            WARNING,
            ERROR,
            FATAL,
            UNKNOW
        };

        // 处理信息格式：信息等级 时间 pid 消息体
        static void handlerMessage(HandlerLevel level, const std::string& file_path, const size_t& line, const char *format, ...)
        {
            char logleft[1024];
            snprintf(logleft, sizeof(logleft), "[%s] ====> [%s][%s][%s][line:%d]", _base_name.c_str(), getlevel(level).c_str(), gettime().c_str(), file_path.c_str(), line);

            char logright[1024];
            va_list ap;
            va_start(ap, format);
            vsnprintf(logright, sizeof(logright), format, ap);
            va_end(ap);
            printf("%s  $  %s\n", logleft, logright);
        }

    private:
        static std::string getlevel(HandlerLevel level)
        {
            switch (level)
            {
            case DEBUG:
                return "DEBUG";
            case INFO:
                return "INFO";
            case WARNING:
                return "WARNING";
            case ERROR:
                return "ERROR";
            case FATAL:
                return "FATAL";
            default:
                return "UNKNOW";
            }
        }

        static std::string gettime()
        {
            time_t currenttime = time(nullptr);
            struct tm *formattime = localtime(&currenttime);
            char buffer[128];
            snprintf(buffer, sizeof(buffer), "%d-%d-%d %d:%d:%d",
                     formattime->tm_year + 1900, formattime->tm_mon + 1, formattime->tm_mday,
                     formattime->tm_hour, formattime->tm_min, formattime->tm_sec);
            return buffer;
        }

    }; // class HandlerMessage

    std::string HandlerMessage::_base_name = "log-system";

} // namespace log_system

#endif // #ifndef __HANDLER_MESSAGE_HPP__
